package com.eam.eventhandler;

import com.eam.data.EamMaintainItem;
import org.openbravo.base.model.Entity;
import org.openbravo.base.model.ModelProvider;
import org.openbravo.base.model.Property;
import org.openbravo.client.kernel.event.EntityDeleteEvent;
import org.openbravo.client.kernel.event.EntityNewEvent;
import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
import org.openbravo.client.kernel.event.EntityUpdateEvent;
import org.openbravo.database.SessionInfo;
import javax.enterprise.event.Observes;

public class MaintainTypeEventHandler extends EntityPersistenceEventObserver {
	private static Entity[] entities = { ModelProvider.getInstance().getEntity(EamMaintainItem.ENTITY_NAME) };

	@Override
	protected Entity[] getObservedEntities() {
		return entities;
	}

	public void onUpdate(@Observes EntityUpdateEvent event) throws Throwable {
		if (!isValidEvent(event)) {
			return;
		}
		
		EamMaintainItem details = (EamMaintainItem) event.getTargetInstance();

		//获取计划负责人
//		String planuser = details.getPlanUser();
//		//获取参与人员
//		String groupuser = details.getGroupUser();

		boolean outcome = true;
		boolean outcomes = true;
		
		//判断参与人员是否都在用户管理存在
//		if(groupuser != null){
//			String [] Groupmembers = groupuser.split(",");
//			int numbers = Groupmembers.length;
//
//			for(int i = 0; i<numbers ; i++){
//				String name = Groupmembers[i];
//
//				String eql = "select m from ADUser m where m.name = :plan_user";
//				Query results = OBDal.getInstance().getSession().createQuery(eql);
//				results.setString("plan_user", name);
//
//				if(results.list().size() <= 0){
//							outcome = false;
//				}
//
//			}
//		}
//
//		//判断计划负责人是否在用户管理（AD_USER）里存在
//		if(planuser != null){
//			String qql = "select m from ADUser m where m.name = :plan_user";
//			Query results = OBDal.getInstance().getSession().createQuery(qql);
//			results.setString("plan_user", planuser);
//
//			if(results.list().size() <= 0){
//				outcomes = false;
//			}
//		}
		
//		if(!outcome){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "参与人员中有人不在用户管理里，请仔细检查！。", language));
//		}
//
//		if(!outcomes){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "计划负责人"+planuser+"不在用户管理里，请仔细检查！。", language));
//		}

		final Property typeProperty = entities[0].getProperty(EamMaintainItem.PROPERTY_TYPE);

		// 获取tabId，用来判断新增的哪种类型
		String tabId = SessionInfo.getProcessId();
		
		if ("4783E430EB84417AABC5859E98AC0804".equalsIgnoreCase(tabId)) {
			event.setCurrentState(typeProperty, "REPAIR"); // 维修项目录入
		} else {
			event.setCurrentState(typeProperty, "MAINTAIN"); // 保养项目录入
		}

		
	}

	public void onSave(@Observes EntityNewEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		
		EamMaintainItem details = (EamMaintainItem) event.getTargetInstance();
		
		//获取计划负责人
//		String planuser = details.getPlanUser();
//		//获取参与人员
//		String groupuser = details.getGroupUser();
		
		boolean outcome = true;
		boolean outcomes = true;
		
		//判断参与人员是否都在用户管理存在
//		if(groupuser != null){
//			String [] Groupmembers = groupuser.split(",");
//			int numbers = Groupmembers.length;
//
//			for(int i = 0; i<numbers ; i++){
//				String name = Groupmembers[i];
//
//				String eql = "select m from ADUser m where m.name = :plan_user";
//				Query results = OBDal.getInstance().getSession().createQuery(eql);
//				results.setString("plan_user", name);
//
//				if(results.list().size() <= 0){
//							outcome = false;
//				}
//
//			}
//		}
//
//
//		if(planuser != null){
//			//判断计划负责人是否在用户管理（AD_USER）里存在
//			String qql = "select m from ADUser m where m.name = :plan_user";
//			Query results = OBDal.getInstance().getSession().createQuery(qql);
//			results.setString("plan_user", planuser);
//
//			if(results.list().size() <= 0){
//				outcomes = false;
//			}
//		}
		
//		if(!outcome){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "参与人员中有人不在用户管理里，请仔细检查！。", language));
//		}
//
//		if(!outcomes){
//			String language = OBContext.getOBContext().getLanguage().getLanguage();
//			ConnectionProvider conn = new DalConnectionProvider(false);
//			throw new OBException(Utility.messageBD(conn, "计划负责人"+planuser+"不在用户管理里，请仔细检查！。", language));
//		}

		final Property typeProperty = entities[0].getProperty(EamMaintainItem.PROPERTY_TYPE);

		// 获取tabId，用来判断新增的哪种类型
		String tabId = SessionInfo.getProcessId();
		
//		D284BFB3DCF04445B96FE4A7841AD310
		
		if ("4783E430EB84417AABC5859E98AC0804".equalsIgnoreCase(tabId)) {
			event.setCurrentState(typeProperty, "REPAIR"); // 维修项目录入
		} else {
			event.setCurrentState(typeProperty, "MAINTAIN"); // 保养项目录入
		}
		
		
	}

	public void onDelete(@Observes EntityDeleteEvent event) {
		if (!isValidEvent(event)) {
			return;
		}
		// logger.info("Greeting " + event.getTargetInstance().getId() +
		// " is being deleted");
	}

}
